
/* eslint-disable */
<template>
  <p-card :bordered="false">

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <p-button @click="handleAdd" type="primary" icon="plus">新增</p-button>
      <p-button type="primary" icon="download" @click="handleExportXls('食物测试表')">导出</p-button>
      <p-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel">
        <p-button type="primary" icon="import">导入</p-button>
      </p-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="fieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <p-dropdown v-if="selectedRowKeys.length > 0">
        <p-menu slot="overlay">
          <p-menu-item key="1" @click="batchDel"><p-icon type="delete"/>删除</p-menu-item>
        </p-menu>
        <p-button style="margin-left: 8px"> 批量操作 <p-icon type="down" /></p-button>
      </p-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <p-table
        ref="table"
        size="middle"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :expandedRowKeys="expandedRowKeys"
        @change="handleTableChange"
        @expand="handleExpand"
        v-bind="tableProps">

        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="图片不存在" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此文件</span>
          <p-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </p-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <p-divider type="vertical" />
          <p-dropdown>
            <a class="ant-dropdown-link">更多 <p-icon type="down" /></a>
            <p-menu slot="overlay">
              <p-menu-item>
                <p-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </p-popconfirm>
              </p-menu-item>
            </p-menu>
          </p-dropdown>
        </span>

      </p-table>
    </div>

    <fxFoodDemo-modal ref="modalForm" @ok="modalFormOk"></fxFoodDemo-modal>
  </p-card>
</template>

<script>

  import { getAction } from '@/api/manage'
  import JSuperQuery from '@/components/jeecg/JSuperQuery.vue'
  import { filterObj } from '@/utils/util'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  // import FxFoodDemoModal from './modules/FxFoodDemoModal'
  import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'

   // 高级查询modal需要参数
    const superQueryFieldList = [
            {
                value: 'createTime',
                text: '创建日期',
                type: 'Date'
            },
            {
                value: 'pid',
                text: '父级节点',
                type: 'string'
            },
            {
                value: 'foodName',
                text: '食物名称',
                type: 'String'
            },
            {
                value: 'remark',
                text: '备注',
                type: 'String'
            },
            {
                value: 'code',
                text: '食物码',
                type: 'String'
            },
    ]

  export default {
    name: 'FxFoodDemoList',
    mixins: [JeecgListMixin],
    components: {
      JSuperQuery,
      // FxFoodDemoModal
    },
    data () {
      return {
        fieldList: superQueryFieldList,
        superQueryFlag: false,
        superQueryParams: '',
        description: '食物测试表管理页面',
        // 表头
        columns: [
          {
            title: '食物名称',
            align: 'left',
            dataIndex: 'foodName'
          },
          {
            title: '创建日期',
            align: 'left',
            dataIndex: 'createTime'
          },
          {
            title: '备注',
            align: 'left',
            dataIndex: 'remark'
          },
          {
            title: '食物码',
            align: 'left',
            dataIndex: 'code'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: '/codedemo/fxFoodDemo/rootList',
          childList: '/codedemo/fxFoodDemo/childList',
          delete: '/codedemo/fxFoodDemo/delete',
          deleteBatch: '/codedemo/fxFoodDemo/deleteBatch',
          exportXlsUrl: '/codedemo/fxFoodDemo/exportXls',
          importExcelUrl: 'codedemo/fxFoodDemo/importExcel',
        },
        expandedRowKeys: [],
        hasChildrenField: 'hasChild',
        pidField: 'pid',
        dictOptions: {}
      }
    },
    computed: {
      importExcelUrl () {
        return `${this.url.importExcelUrl}`
      },
      tableProps () {
        const _this = this
        return {
          // 列表项是否可选择
          rowSelection: {
            selectedRowKeys: _this.selectedRowKeys,
            onChange: (selectedRowKeys) => _this.selectedRowKeys = selectedRowKeys
          }
        }
      }
    },
    methods: {
        // 高级查询方法
        handleSuperQuery (arg) {
            if (!arg) {
                this.superQueryParams = ''
                this.superQueryFlag = false
            } else {
                this.superQueryFlag = true
                this.superQueryParams = JSON.stringify(arg)
            }
            this.loadData()
        },
        // 获取查询条件
        getQueryParams () {
            const sqp = {}
            if (this.superQueryParams) {
                sqp['superQueryParams'] = encodeURI(this.superQueryParams)
            }
            var param = Object.assign(sqp, this.queryParam, this.isorter)
            param.field = this.getQueryField()
            param.pageNo = this.ipagination.current
            param.pageSize = this.ipagination.pageSize
            return filterObj(param)
        },
      loadData (arg) {
        if (arg == 1) {
          this.ipagination.current = 1
        }
        this.loading = true
        this.expandedRowKeys = []
        const params = this.getQueryParams()
        return new Promise((resolve) => {
          getAction(this.url.list, params).then(res => {
            if (res.success) {
              const result = res.data
              if (Number(result.total) > 0) {
                this.ipagination.total = Number(result.total)
                this.dataSource = this.getDataByResult(res.data.records)
                resolve()
              } else {
                this.ipagination.total = 0
                this.dataSource = []
              }
            } else {
              this.$message.warning(res.msg)
            }
            this.loading = false
          })
        })
      },
      getDataByResult (result) {
        return result.map(item => {
          // 判断是否标记了带有子节点
          if (item[this.hasChildrenField] == '1') {
            const loadChild = { id: item.id + '_loadChild', name: 'loading...', isLoading: true }
            item.children = [loadChild]
          }
          return item
        })
      },
      handleExpand (expanded, record) {
        // 判断是否是展开状态
        if (expanded) {
          this.expandedRowKeys.push(record.id)
          if (record.children.length > 0 && record.children[0].isLoading === true) {
            const params = this.getQueryParams()// 查询条件
            params[this.pidField] = record.id
            getAction(this.url.childList, params).then((res) => {
              if (res.success) {
                if (res.data && res.data.length > 0) {
                  record.children = this.getDataByResult(res.data)
                  this.dataSource = [...this.dataSource]
                } else {
                  record.children = ''
                  record.hasChildrenField = '0'
                }
              } else {
                this.$message.warning(res.msg)
              }
            })
          }
        } else {
          const keyIndex = this.expandedRowKeys.indexOf(record.id)
          if (keyIndex >= 0) {
            this.expandedRowKeys.splice(keyIndex, 1)
          }
        }
      },
      initDictConfig () {
      },
      modalFormOk (formData, arr) {
        if (!formData.id) {
          this.addOk(formData, arr)
        } else {
          if (!arr) {
            this.loadData()
          } else {
            this.editOk(formData, this.dataSource)
            this.dataSource = [...this.dataSource]
          }
        }
      },
      editOk (formData, arr) {
        if (arr && arr.length > 0) {
          for (let i = 0; i < arr.length; i++) {
            if (arr[i].id == formData.id) {
              arr[i] = formData
              break
            } else {
              this.editOk(formData, arr[i].children)
            }
          }
        }
      },
      async addOk (formData, arr) {
        if (!formData[this.pidField]) {
          this.loadData()
        } else {
          this.expandedRowKeys = []
          for (const i of arr) {
            await this.expandTreeNode(i)
          }
        }
      },
      expandTreeNode (nodeId) {
        return new Promise((resolve, reject) => {
          this.getFormDataById(nodeId, this.dataSource)
          const row = this.parentFormData
          this.expandedRowKeys.push(nodeId)
          const params = this.getQueryParams()// 查询条件
          params[this.pidField] = nodeId
          getAction(this.url.childList, params).then((res) => {
            if (res.success) {
              if (res.data && res.data.length > 0) {
                row.children = this.getDataByResult(res.data)
                this.dataSource = [...this.dataSource]
                resolve()
              } else {
                reject()
              }
            } else {
              reject()
            }
          })
        })
      },
      getFormDataById (id, arr) {
        if (arr && arr.length > 0) {
          for (let i = 0; i < arr.length; i++) {
            if (arr[i].id == id) {
              this.parentFormData = arr[i]
            } else {
              this.getFormDataById(id, arr[i].children)
            }
          }
        }
      },

    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
